package com.shmn.gongjiajumain.controller;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.shmn.gongjiajumain.entity.JigDefinitionEntity;
import com.shmn.gongjiajumain.vo.jig.JigDetailVo;
import com.shmn.gongjiajumain.vo.jig.JigTableVo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.shmn.gongjiajumain.service.JigDefinitionService;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.R;



/**
 * 记录工夹具的定义信息
 *
 * @author chenshun
 * @email 1005074582@qq.com
 * @date 2023-05-14 16:40:28
 */
@RestController
@RequestMapping("gongjiajumain/jigdefinition")
public class JigDefinitionController {
    @Autowired
    private JigDefinitionService jigDefinitionService;


    @PostMapping("/exportExcel")
    public ResponseEntity<byte[]> exportExcel(@RequestBody List<JigTableVo> list) {
        try (Workbook wb = new XSSFWorkbook()) {
            Sheet sheet = wb.createSheet("Sheet1");
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("工夹具代码");
            headerRow.createCell(1).setCellValue("工夹具名称");
            headerRow.createCell(2).setCellValue("所属部门");
            headerRow.createCell(3).setCellValue("类别");
            headerRow.createCell(4).setCellValue("用途");
            headerRow.createCell(5).setCellValue("库存数量");

            int rowNum = 1;
            for (JigTableVo model : list) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(model.getCode());
                row.createCell(1).setCellValue(model.getName());
                row.createCell(2).setCellValue(model.getWorkcell());
                row.createCell(3).setCellValue(model.getFamily());
                row.createCell(4).setCellValue(model.getUserFor());
                row.createCell(5).setCellValue(model.getStockCount());
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            wb.write(baos);
            byte[] data = baos.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "exported_data.xlsx");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(data, headers, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException("导出Excel失败！");
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gongjiajumain:jigdefinition:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jigDefinitionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gongjiajumain:jigdefinition:info")
    public R info(@PathVariable("id") Integer id){
		JigDefinitionEntity jigDefinition = jigDefinitionService.getById(id);

        return R.ok().put("jigDefinition", jigDefinition);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gongjiajumain:jigdefinition:save")
    public R save(@RequestBody JigDefinitionEntity jigDefinition){
		jigDefinitionService.save(jigDefinition);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gongjiajumain:jigdefinition:update")
    public R update(@RequestBody JigDefinitionEntity jigDefinition){
		jigDefinitionService.updateById(jigDefinition);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gongjiajumain:jigdefinition:delete")
    public R delete(@RequestBody Integer[] ids){
		jigDefinitionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
